package com.zhiyou.app.common.starejob;

import com.zhiyou.app.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class ApplicationStartJobRunner implements ApplicationRunner {
    @Resource
    private List<StartJob> startJobs;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        startJobs.forEach(startJob -> {
            try {
                startJob.run();
            } catch (Exception e) {
                log.error("启动" + startJob.getClass().getSimpleName() + "任务失败", e);
                throw new BizException("启动" + startJob.getClass().getSimpleName() + "任务失败");
            }
        });
    }
}
